import json
from enum import Enum
from flask import Response
from multipledispatch import dispatch

class HttpStatus(Enum):
    success = [200, '成功']
    fail = [500, '失败']

    @property
    def code(self):
        return self.value[0]

    @property
    def msg(self):
        return self.value[1]

@dispatch(str, int, object)
def result(msg :str, code :int, data :object=None):
    if data is None:
        data = {}
    return json.dumps({'msg': msg, 'code': code, 'data': data}, ensure_ascii=False, default=lambda obj: obj.__dict__)

@dispatch(HttpStatus, object)
def result(http_status : HttpStatus, data :object=None):
    return result(http_status.msg, http_status.code, data)

def success(data: object=None):
    return result(HttpStatus.success, data)

def fail():
    return result(HttpStatus.fail)